package com.yang.leather.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yang.leather.admin.dao.IProdDao;
import com.yang.leather.admin.pojo.Product;
import com.yang.leather.admin.service.IProdService;
import com.yang.leather.common.page.Pagination;
import com.yang.leather.common.util.ComUtils;
import com.yang.leather.core.LeatherCoreServiceImpl;

@Service("prodService")
@Transactional
public class ProdServiceImpl extends LeatherCoreServiceImpl<Product> implements IProdService{
	
	public Pagination getProductListPage(Map<String, String> param, int pageNo, int pageSize) {

		Map<String,Object> queryObj = new HashMap<String,Object>();
		if(param!=null && param.size()>0){
			for(Map.Entry<String, String> entry : param.entrySet()){
				if(entry.getValue()!=null && !"".equals(entry.getValue())){
					if("prodName".equals(entry.getKey())){
						queryObj.put("prodName", "%"+param.get("prodName")+"%");
					}else if("typeId".equals(entry.getKey())){
						queryObj.put("prodType.typeId", Long.valueOf(param.get("typeId")));
					}
				}
			}
		}
		return getDao().getProductListPage(queryObj, pageNo, pageSize);
	}
	
	public List<Product> getLimitProdList(Map<String, String> param, int limit) {
		Map<String,Object> queryObj = new HashMap<String,Object>();
		if(param!=null && param.size()>0){
			for(Map.Entry<String, String> entry : param.entrySet()){
				if(entry.getValue()!=null && !"".equals(entry.getValue())){
					if("prodName".equals(entry.getKey())){
						queryObj.put("prodName", "%"+param.get("prodName")+"%");
					}
				}
			}
		}
		
		return getDao().getLimitProdList(queryObj, limit);
	}
	
	public Product findProductById(Long id) {
		return this.getDao().get(id);
	}
	
	public int countProductList(Map<String, String> param) {
		Map<String,Object> queryObj = new HashMap<String,Object>();
		if(param!=null && param.size()>0){
			for(Map.Entry<String, String> entry : param.entrySet()){
				if(entry.getValue()!=null && !"".equals(entry.getValue())){
					if("prodName".equals(entry.getKey())){
						queryObj.put("prodName", "%"+param.get("prodName")+"%");
					}
				}
			}
		}
		return this.getDao().countProductList(queryObj);
	}

	public void saveProduct(Product product) {
		product.setCreateDate(ComUtils.now());
		product.setUpdDate(ComUtils.now());
		this.getDao().save(product);
	}

	public void updateProduct(Product product) {
		product.setUpdDate(ComUtils.now());
		this.getDao().update(product);
	}

	public void updateStatus(int status, List<Long> ids) {
		for(Long id : ids){
			Product product = this.getDao().get(id);
			product.setStatus(status);
			this.getDao().update(product);
		}
	}
	
	public void batchDel(List<Long> ids) {
		for(Long id : ids){
			super.deleteById(id);
		}
	}

	public void deleteById(Long id) {
		super.deleteById(id);
	}

	@Autowired
	public void setDao(@Qualifier("prodDao")
			IProdDao dao){
		super.setDao(dao);
	}
	
	public IProdDao getDao(){
		return (IProdDao)super.getDao();
	}
	
}
